package com.szx.usercenter.handle;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.szx.usercenter.util.Response;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author songzx
 * @date 2024/5/12
 * @apiNote
 */
@RestControllerAdvice
@Log4j2
public class GlobalExceptionHandler {
    // 全局异常处理
    // @ExceptionHandler(Exception.class)
    // @ResponseBody
    // public Response<Object> error(Exception e){
    //     log.error(ExceptionUtil.getMessage(e));
    //     e.printStackTrace();
    //     // 将异常转成string返回出去
    //     return Response.error(e.getMessage());
    // }


    /**
     * 处理自定义的异常-CenterExceptionHandler
     * @param e
     * @return
     */
    @ExceptionHandler(CenterExceptionHandler.class)
    @ResponseBody
    public Response<Object>  businessExceptionHandler(CenterExceptionHandler e){
        log.error("CenterExceptionHandler:" + e.getMessage(),e);
        return Response.rspMsg(e.getCode(),e.getMessage());
    }
}
